package com.qizhu.web;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qizhu.entity.User;
import com.qizhu.service.IUserService;


/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author 张权
 * @since 2017-04-18
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController{
	
	@Autowired
	 private IUserService userService;


    @RequestMapping("/")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("index");
        modelAndView.addObject("userList", userService.selectList(null));
        return modelAndView;
    }

	    @RequestMapping("/preSave")
	    public ModelAndView preSave(ModelAndView modelAndView, @RequestParam(value = "id", required = false) Long id) {
	        modelAndView.setViewName("save");
	        if (id != null) {
	            modelAndView.addObject("user", userService.selectById(id));
	        }
	        return modelAndView;
	    }

	    @ResponseBody
	    @RequestMapping("save")
	    public Object save(User user) {
	    	Map<String,Object> map = new HashMap<>();
	        if (user.getId() == null) {
	        	map.put("user", user);
	            return userService.insert(user) ? renderSuccess("添加成功") : renderError("添加失败");
	        } else {
	            return userService.updateById(user) ? renderSuccess("修改成功") : renderError("修改失败");
	        }
	    }

	    @ResponseBody
	    @RequestMapping("/delete")
	    public Object delete(@RequestParam(value = "id", required = false) Long id) {
	        return userService.deleteById(id) ? renderSuccess("删除成功") : renderError("删除失败");
	    }
}
